package com.itcast.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.itcast.pojo.Goods;
import com.itcast.pojo.User;
import com.itcast.service.Goodsservice;
import com.itcast.utils.Respone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import sun.rmi.runtime.Log;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-19
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private Goodsservice goodsservice;

    @RequestMapping("/index1")
    @ResponseBody
    public Map<String,Object> index() throws ParseException {
        Map<String, Object> map=new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        List<String> list1=new ArrayList<>();
        List<BigDecimal> list2=new ArrayList<>();
        List<Long> list3=new ArrayList<>();
        for (int i = 0; i <=3; i++) {
            Map<String, Object> countgoods = goodsservice.countgoods(i);
            if (countgoods.get("creationTime")!=null){
                String creationTime =  countgoods.get("creationTime").toString();
                System.out.println(creationTime+"==================");
                Date parse = sdf.parse(creationTime);
                String format = sdf.format(parse);
                list1.add(format);
                map.put("date", list1);
                BigDecimal views = (BigDecimal) countgoods.get("SUM(views)");
                list2.add(views);
                map.put("size2", list2);
                Long goods = (Long) countgoods.get("count(creationTime)");
                list3.add(goods);
                map.put("size3", list3);
            }else {
                list1.add("");
                map.put("date", list1);
                list2.add(BigDecimal.valueOf(0));
                map.put("size2", list2);
                list3.add(Long.valueOf(0));
                map.put("size3", list3);
            }
        }
        return map;
    }


    @GetMapping("/list")
    public Respone list(@RequestParam("page") int page, @RequestParam("limit") int limit){
        QueryWrapper<Goods> queryWrapper=new QueryWrapper<>();
        Page<Goods> pageSize = new Page<>(page, limit);
        IPage<Goods> page1 = goodsservice.page(pageSize, queryWrapper);
        System.out.println((int)page1.getTotal());
        return new Respone(0,"成功",page1.getRecords(),(int)page1.getTotal());
    }

    @PostMapping("/update")
    public Respone update(@RequestBody Goods Goods){
        boolean update = goodsservice.updateById(Goods);
        if(update){
            return new Respone(0,"更新成功");
        }
        return new Respone(999,"更新失败");
    }

    @GetMapping("/delete")
    public Respone del(@RequestParam("gid") Integer gid){
        if(  goodsservice.removeById(gid)){
            return new Respone(0,"删除成功");
        }
        return new Respone(999,"删除失败");
    }

}

